﻿using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;

using TRPOP.CMS.Model;
using TRPOP.CMS.Business;
using TRPOP.CMS.Plugin;
using TRPOP.CMS.Log;
using System.IO;
using System.Text.RegularExpressions;
using System.Text;
using TRPOP.Common;

namespace TRPOP.CMS.Web.UI
{
    /// <summary>
    /// 
    /// </summary>
    public class BasePage : System.Web.UI.Page
    {
        #region 方法重写
        //protected override void Render(HtmlTextWriter oHtmlTextWriter)
        //{
            //base.Render(oHtmlTextWriter);
            //StringWriter oStringWriter = new StringWriter();
            //HtmlTextWriter oHtmlTextWriterNew = new HtmlTextWriter(oStringWriter);
            //base.Render(oHtmlTextWriterNew);//把页面生成内容拿出来 
            //oHtmlTextWriter.Write(TRPOP.Common.CommonRegex.CutHtmlRedundanceSpace(oStringWriter.ToString()));
        //}
        #endregion

        #region 警告框 Alert
        /// <summary>
        /// 弹出景高层
        /// </summary>
        /// <param name="strmessage"></param>
        public void AlertDialog(string strmessage)
        {
            PageHelper.ExecuteJs(this.Page, "ShowError(\"" + strmessage + "\");");
        }

        /// <summary>
        /// 警告框
        /// </summary>
        /// <param name="mess"></param>
        public void Alert(string mess)
        {
            PageHelper.Alert(this.Page, mess);
        }

        /// <summary>
        /// 警告框,然后转向
        /// </summary>
        /// <param name="mess"></param>
        /// <param name="url"></param>
        public void Alert(string mess, string url)
        {
            PageHelper.Alert(this.Page, mess, url);
        }
        #endregion

        #region  弹出窗口操作方法
        /// <summary>
        /// 弹出窗口时刷新父页面
        /// </summary>
        public void ParentReset()
        {
            //PageHelper.ExecuteJs(this.Page, "parent.reset();");
            Response.Write("<script>parent.reset();</script>");
            Response.End();
        }
        /// <summary>
        /// 弹出窗口时，关闭弹出页
        /// </summary>
        public void ParentClose()
        {
            //PageHelper.ExecuteJs(this.Page, "parent.close_dialog();");
            Response.Write("<script>parent.close_dialog();</script>");
            Response.End();
        }
        /// <summary>
        /// 执行一段JS代码
        /// </summary>
        /// <param name="js"></param>
        public void ExecuteJs(string js)
        {
            PageHelper.ExecuteJs(this.Page, js);
        }
        /// <summary>
        /// 执行一段JS代码
        /// </summary>
        /// <param name="js"></param>
        public void ExecuteJsWidthEnd(string js)
        {
            Response.Write("<script>"+js+"</script>");
            Response.End();
        }

        #endregion

        #region 日志管理
        /// <summary>
        /// 错误日志记录
        /// </summary>
        /// <param name="e"></param>
        protected override void OnError(EventArgs e)
        {
            Exception ex = Server.GetLastError();
            TRPOP.CMS.Business.LogManage oLogManage = new TRPOP.CMS.Business.LogManage();
            TRPOP.CMS.Model.LogInfo oLogInfo = new TRPOP.CMS.Model.LogInfo();
            oLogInfo.AddTime = DateTime.Now;
            TRPOP.CMS.UserManage.AdminUserInfo m_UserBaseInfo = (TRPOP.CMS.UserManage.AdminUserInfo)IAdminUserAccess.GetInstance().getUserBaseInfo();
            if(m_UserBaseInfo==null)
            {
                m_UserBaseInfo = new TRPOP.CMS.UserManage.AdminUserInfo();
                m_UserBaseInfo.UserBaseId = 0;
            }
            oLogInfo.AddUserId = m_UserBaseInfo.UserBaseId;
            oLogInfo.Detail = TRPOP.CMS.Plugin.ILogAccess.GetInstance().SysLog(ex, new object[] { ex.Message, ex.Source, ex.StackTrace });
            oLogInfo.Type = (int)LogKeys.SysLogType.PageError;
            oLogManage.Save(oLogInfo);            
            base.OnError(e);
        }
        /// <summary>
        /// 操作日志
        /// </summary>
        /// <param name="action"></param>
        protected void SaveLog(TRPOP.CMS.Plugin.LogKeys.OperObject action,TRPOP.CMS.Plugin.LogKeys.OperResult result)
        {
            ILog Log = ILogAccess.GetInstance();
            Log.SaveLog(action.ToString()+":"+result.ToString());   
        }
        /// <summary>
        /// 操作日志
        /// </summary>
        /// <param name="action"></param>
        /// <param name="result"></param>
        /// <param name="parm">附加参数1</param>
        protected void SaveLog(TRPOP.CMS.Plugin.LogKeys.OperObject action, TRPOP.CMS.Plugin.LogKeys.OperResult result,string parm)
        {
            ILog Log = ILogAccess.GetInstance();
            Log.SaveLog(action.ToString() + ":" + result.ToString()+":"+parm);
        }
        #endregion

        #region Response.End() Response.Redirect 重写
        /// <summary>
        /// 页面跳转
        /// </summary>
        /// <param name="strURL"></param>
        public void Redirect(string strURL)
        {
            Response.Redirect(strURL, false);
            /*
             * 为了避免出现以下异常:
             * 在 System.Threading.ThreadAbortException 中第一次偶然出现的“mscorlib.dll”类型的异常
             *“System.Threading.ThreadAbortException”类型的异常在 mscorlib.dll 中发生，但未在用户代码中进行处理
            */
        }
        /// <summary>
        /// 当前页面停止输出
        /// </summary>
        public void End()
        {
            Response.End();
        }
        /// <summary>
        /// 页面执行
        /// </summary>
        /// <param name="strURL"></param>
        public void Transfer(string strURL)
        {
            Server.Execute(strURL);
            /*
             * 为了避免出现以下异常:
             * 在 System.Threading.ThreadAbortException 中第一次偶然出现的“mscorlib.dll”类型的异常
             *“System.Threading.ThreadAbortException”类型的异常在 mscorlib.dll 中发生，但未在用户代码中进行处理
            */
        }
        #endregion

    }
}
